# mtm78, really simple logging module


import datetime

LOGLEVEL = ['DEBUG', 'INFO', 'WARNING', 'ERROR']


def info(m, level='[INFO]    ', _info=''):
    _logger.log(level + str(m), _info)


def debug(m, _info=''):
    if _logger.currentloglevel == LOGLEVEL[1]:
        info(m, '[DEBUG    :- ]', _info)


def warn(m, _info=''):
    info(m, '[WARNING] :- ', _info)


def err(m, _info=''):
    info(m, '[ERROR]   :- ', _info)


class _Logger(object):
    _logLevel = LOGLEVEL[2]

    def __init__(self):
        self.logfile = open('multihitlog.log', 'w', 1)
        self.log('Starting MultiHitLog logger module')

    def __del__(self):
        self.logfile.close()

    def log(self, m, _info=''):
        try:
            date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ': '

            m = date + m
            if not m.endswith("\n"):
                m += '\n'
            if not _info == '':
                m += date + 'Extra info :- ' + _info + '\n'
            self.logfile.write(m)
        finally:
            pass

    def setloglevel(self, loglevel=2):
        if loglevel is not None:
            self._logLevel = LOGLEVEL[loglevel]

    def currentloglevel(self):
        return self._logLevel


_logger = _Logger()
